jQuery(function($){
BS = {
	getActivePanel: function(element) {
		var $linkTipCtn = $('#linkTipCtn');
		if ($linkTipCtn.is(':visible')) {
			return $linkTipCtn;
		}
		
		var $mainPanel = $('#g_mainPanel');
		if ($mainPanel.length == 0) {
			return $('body');
		} else {
			var selected = $mainPanel.tabs("option", "selected");
			var targetPanelId = $('ul:first>li', $mainPanel).eq(selected).children('a').attr('href');
			if (element) {
				return $(element, $(targetPanelId));
			} else if (targetPanelId) {
				return $(targetPanelId);
			} else {
				return $('body');
			}
		}
	},
	
	initTableGridEvent: function($table, required, sortable, stats) {
		if ($table.length > 1) {			// current tab has the higher priority
			var $curTab = this.getActivePanel();
			$table.each(function() {
				var $cur = $curTab.find('[fullName="' + $(this).parent().attr('fullName') + '"]');
				if ($cur.length == 1) {
					$table = $cur.children('table');
					return false;
				}
			});
		}
		
		this.addNewRow($table);
		$('>tbody>tr:first', $table).hide().find('*').filter('[name]').each(function() {
			$(this).attr('fullName', $(this).attr('name'));
		});
		
		var statsFunc = function(obj) {
			var $obj = $(obj);
			var name = $obj.attr('name') || $obj.attr('txtname');
			var found = false;
			for (var i = 0; i < stats.length; i++) {
				if (stats[i] == name) {
					found = true;
					break;
				}
			}
			if (found) {
				// calculate this row
				var count = 0;
				var $td = $obj.closest('td').prevAll('td:visible');
				var index = $td.length;
				var $row = $table.children('tbody').children('tr');
				$row.each(function(rowIndex) {
					if (rowIndex > 0) {						
						var $cell = $(this).children('td:visible').eq(index);
						var $input = $cell.find('input[type=text]');
						var theVal;
						if ($input.length == 0) {
							theVal = $cell.find('.readonly').text();
						} else {
							theVal = $input.val();
						}
						var nValue = toNumber(theVal);
						count += nValue;
					}
				});
				$table.children('tfoot').children().children().eq(index).html(count);
			}
		};
		$('tr', $table).click(function() {
			var $oldTr = $table.data('$oldTr');
			var $this = $(this);
			$this.addClass('selected');
			if ($oldTr != null) {
				$oldTr.removeClass('selected');
			}
			$table.data('$oldTr', $this);
		});
		$table.on('blur', 'input,textarea,select', function() {
			$(this).parent('.jbox').removeClass('focus');
			var changed = $table.data('changed');
			if (!changed) {
				return;
			}
			var $curRow = $(this).closest('tr');
			var $rows = $('>tr', $curRow.closest('tbody'));
			var index = $rows.index($curRow);
			
			if ((index == $rows.length - 1) && hasRecordValue($curRow)) {
				addNewRow($table, true);
			}
		}).on('focus', 'input,textarea,select', function() {
			$(this).parent('.jbox').addClass('focus');
		}).on('change', 'input,textarea,select,.readonly', function(event) {
			$table.data('changed', true);
			if (stats) {
				statsFunc(this);
			}
		}).on('rowChange', function(event) {
			if (stats) {
				var $row1 = $table.children('tbody').children().eq(1);
				$row1.children().each(function() {
					var $child = $(this).find('input,textarea,select,.readonly').eq(0);
					statsFunc($child.get(0));
				});
			}
		}).on('keypress','td', function(event) {			// use up/down/left/right focus the input element
			// left: 37, up: 38, right: 39, down: 40
			var keyCode = event.keyCode;
			if (keyCode >= 37 && keyCode <= 40) {
				var $td = $(this);
			    if (keyCode == 37) {
			    	var found = false;
			    	while (!found) {
			    		$td = $td.prev('td');
			    		found = $td.length == 0 || $td.find('input,textarea,select').length > 0;
			    	}
			    } else if (keyCode == 39) {
			    	var found = false;
			    	while (!found) {
			    		$td = $td.next('td');
			    		found = $td.length == 0 || $td.find('input,textarea,select').length > 0;
			    	}
			    } else {
			    	var $tr = $td.closest('tr');
			    	if (keyCode == 38) {
			    		$tr = $tr.prev('tr');
			    	} else {
			    		$tr = $tr.next('tr');	
			    	}
			    	var colIndex = $td.index();
			    	$td = $tr.children('td').eq(colIndex);
			    } 
			    $td.find('input,textarea,select').focus();
			}
		});
		if (required) {
			// see initValidate(), $table.attr('required') 
		}
		if (sortable) {
			$table.children('thead').find('th').eq(0).width(25);
			$table.children('tbody').sortable({
				distance: 5,
				items:'tr:not(.templateRow)'
			});
		}
	},
	addNewRow: function($table, initValidateEvent) {
		var $rows = $('>tbody>tr', $table);
		var $clonedRow = $rows.eq(0).clone(true);
		$clonedRow.removeClass('templateRow');
		$('tbody', $table).append($clonedRow);
		$clonedRow.show();
		$table.data('changed', false);
		return $clonedRow;
	}
}
});